package com.data.service.manage.impl;

import com.data.domain.manage.Riskcheck;
import com.data.domain.manage.RiskcheckCriteria;
import com.data.domain.manage.Riskcheck;
import com.data.domain.manage.RiskcheckCriteria;
import com.data.domain.report.PrtryinfoCriteria;
import com.data.mapper.manage.RiskcheckMapper;
import com.data.mapper.report.PrtryinfoMapper;
import com.data.model.ManagerModel;
import com.data.service.manage.RiskcheckService;
import com.data.utill.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service(value = "riskcheckService")
public class RiskcheckServiceImpl implements RiskcheckService {

    @Autowired
    private RiskcheckMapper riskcheckMapper;

    @Autowired
    private PrtryinfoMapper prtryinfoMapper;

    @Override
    public List<Riskcheck> getRiskcheck(ManagerModel managerModel) {
        RiskcheckCriteria riskcheckCriteria=new RiskcheckCriteria();
        RiskcheckCriteria.Criteria criteria=riskcheckCriteria.createCriteria();
        if(StringUtils.isNoNullString(managerModel.getOrgcode())){
            criteria.andOrgcodeEqualTo(managerModel.getOrgcode());
        }
        if(StringUtils.isNoNullString(managerModel.getPrtry())){
            criteria.andPrtryEqualTo(managerModel.getPrtry());
        }
        if (managerModel.getOrgcodelist().size()>0 && !managerModel.getAreacode().equals("3300")){
            criteria.andOrgcodeIn(managerModel.getOrgcodelist());
        }
        riskcheckCriteria.setOrderByClause("id asc");
        List<Riskcheck> data = new ArrayList<>();
        for (Riskcheck riskcheck:riskcheckMapper.selectByExample(riskcheckCriteria)){
            PrtryinfoCriteria prtryinfoCriteria = new PrtryinfoCriteria();
            prtryinfoCriteria.createCriteria().andCodeEqualTo(riskcheck.getPrtry());
            riskcheck.setPrtry(prtryinfoMapper.selectByExample(prtryinfoCriteria).get(0).getName());
            data.add(riskcheck);
        }
        return data;
    }

    /**
     * 入网机构信息插入
     * @param managerModel
     * @return
     */
    @Override
    public int insertRiskcheck(ManagerModel managerModel) {
        RiskcheckCriteria riskcheckCriteria=new RiskcheckCriteria();
        Riskcheck data = new Riskcheck();
//        data.setOrgcode(managerModel.getOrgcode());
        data.setTrncode(managerModel.getTrncode());
        data.setPrtry(managerModel.getPrtry());
        data.setCheckconsign(Short.parseShort(managerModel.getCheckconsign()));
        data.setCheckamt(Integer.parseInt(managerModel.getCheckamt()));
        data.setMinamt(Integer.parseInt(managerModel.getMinamt()));
        data.setMaxamt(Long.parseLong(managerModel.getMaxamt()));
        data.setDayamt(Long.parseLong(managerModel.getDayamt()));
        data.setMonthamt(Long.parseLong(managerModel.getMonthamt()));
        return riskcheckMapper.insertSelective(data);
    }

    /**
     * 入网机构信息修改
     * @param managerModel
     * @return
     */
    @Override
    public int modifyRiskcheck(ManagerModel managerModel ,String id) {
        RiskcheckCriteria riskcheckCriteria=new RiskcheckCriteria();
        riskcheckCriteria.createCriteria().andOrgcodeEqualTo(id);
        Riskcheck data = new Riskcheck();
//        data.setOrgcode(managerModel.getOrgcode());
        data.setTrncode(managerModel.getTrncode());
        data.setPrtry(managerModel.getPrtry());
        data.setCheckconsign(Short.parseShort(managerModel.getCheckconsign()));
        data.setCheckamt(Integer.parseInt(managerModel.getCheckamt()));
        data.setMinamt(Integer.parseInt(managerModel.getMinamt()));
        data.setMaxamt(Long.parseLong(managerModel.getMaxamt()));
        data.setDayamt(Long.parseLong(managerModel.getDayamt()));
        data.setMonthamt(Long.parseLong(managerModel.getMonthamt()));
        return riskcheckMapper.updateByExampleSelective(data,riskcheckCriteria);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Riskcheck getRiskcheckById(String  id) {
        RiskcheckCriteria riskcheckCriteria=new RiskcheckCriteria();
        riskcheckCriteria.createCriteria().andOrgcodeEqualTo(id);
        List<Riskcheck> data = riskcheckMapper.selectByExample(riskcheckCriteria);
        if (null!=data) {
            return data.get(0);
        }else{
            return null;
        }
    }
}
